<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="###Android AccessibilityService无障碍服务  当服务未开启时，快速的跳转到开启服务的界面。123456if (!OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(this,            AccessibilitySampleService.class.getName()))&amp;#123;// 判断">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-AccessibilityService无障碍服务（二）">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Android/Android-AccessibilityService无障碍服务（二）/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="###Android AccessibilityService无障碍服务  当服务未开启时，快速的跳转到开启服务的界面。123456if (!OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(this,            AccessibilitySampleService.class.getName()))&amp;#123;// 判断">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T11:19:14.085Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-AccessibilityService无障碍服务（二）">
<meta name="twitter:description" content="###Android AccessibilityService无障碍服务  当服务未开启时，快速的跳转到开启服务的界面。123456if (!OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(this,            AccessibilitySampleService.class.getName()))&amp;#123;// 判断">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Android/Android-AccessibilityService无障碍服务（二）/"/>





  <title>Android-AccessibilityService无障碍服务（二） | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Android/Android-AccessibilityService无障碍服务（二）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-AccessibilityService无障碍服务（二）</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>###Android AccessibilityService无障碍服务</p>
<ol>
<li>当服务未开启时，快速的跳转到开启服务的界面。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">if (!OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(this,</span><br><span class="line">            AccessibilitySampleService.class.getName()))&#123;// 判断服务是否开启</span><br><span class="line">          OpenAccessibilitySettingHelper.jumpToSettingPage(this);// 跳转到开启页面</span><br><span class="line">        &#125;else &#123;</span><br><span class="line">          Toast.makeText(this, &quot;服务已开启&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<a id="more"></a>
<p>用到的方法具体实现：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 开启无障碍服务帮助类</span><br><span class="line"> * Created by mazaiting on 2017/8/18.</span><br><span class="line"> */</span><br><span class="line">public class OpenAccessibilitySettingHelper &#123;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 跳转到无障碍服务设置页面</span><br><span class="line">   * @param context 设备上下文</span><br><span class="line">   */</span><br><span class="line">  public static void jumpToSettingPage(Context context)&#123;</span><br><span class="line">    Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);</span><br><span class="line">    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</span><br><span class="line">    context.startActivity(intent);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 判断是否有辅助功能权限</span><br><span class="line">   * @return true 已开启</span><br><span class="line">   *          false 未开启</span><br><span class="line">   */</span><br><span class="line">  public static boolean isAccessibilitySettingsOn(Context context,String className)&#123;</span><br><span class="line">    if (context == null)&#123;</span><br><span class="line">      return false;</span><br><span class="line">    &#125;</span><br><span class="line">    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);</span><br><span class="line">    List&lt;ActivityManager.RunningServiceInfo&gt; runningServices =</span><br><span class="line">        activityManager.getRunningServices(100);// 获取正在运行的服务列表</span><br><span class="line">    if (runningServices.size()&lt;0)&#123;</span><br><span class="line">      return false;</span><br><span class="line">    &#125;</span><br><span class="line">    for (int i=0;i&lt;runningServices.size();i++)&#123;</span><br><span class="line">      ComponentName service = runningServices.get(i).service;</span><br><span class="line">      if (service.getClassName().equals(className))&#123;</span><br><span class="line">        return true;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return false;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>2.模拟点击，创建模拟点击的Activity为AccessibilityNormalSampleActivity，并在AndroidManifest.xml将AccessibilityNormalSampleActivity与AccessibilitySampleService配置在同一个进程，若不在同一进程，则获取到的AccessibilityService与AccessibilityEvent为空。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">android:process=&quot;:BackgroundService&quot;</span><br></pre></td></tr></table></figure></p>
<p>配置文件为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 注册辅助功能服务 --&gt;</span><br><span class="line">    &lt;service</span><br><span class="line">        android:name=&quot;.service.AccessibilitySampleService&quot;</span><br><span class="line">        android:enabled=&quot;true&quot;</span><br><span class="line">        android:exported=&quot;true&quot;</span><br><span class="line">        android:label=&quot;@string/accessibility_tip&quot;</span><br><span class="line">        android:permission=&quot;android.permission.BIND_ACCESSIBILITY_SERVICE&quot;</span><br><span class="line">        android:process=&quot;:BackgroundService&quot;&gt;</span><br><span class="line"></span><br><span class="line">      &lt;!-- android:label=&quot;@string/accessibility_tip&quot; 在设置中显示的文字 --&gt;</span><br><span class="line">      &lt;intent-filter&gt;</span><br><span class="line">        &lt;action android:name=&quot;android.accessibilityservice.AccessibilityService&quot; /&gt;</span><br><span class="line">      &lt;/intent-filter&gt;</span><br><span class="line">      &lt;!-- 通过xml文件完成辅助功能相关配置，也可以在onServiceConnected中动态配置 --&gt;</span><br><span class="line">      &lt;meta-data</span><br><span class="line">          android:name=&quot;android.accessibilityservice&quot;</span><br><span class="line">          android:resource=&quot;@xml/accessibility_config&quot; /&gt;</span><br><span class="line">    &lt;/service&gt;</span><br><span class="line"></span><br><span class="line">    &lt;activity android:name=&quot;.ui.AccessibilityNormalSampleActivity&quot;</span><br><span class="line">        android:process=&quot;:BackgroundService&quot;&gt;&lt;/activity&gt;</span><br></pre></td></tr></table></figure></p>
<p>AccessibilityNormalSampleActivity界面布局：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:id=&quot;@+id/activity_accessibility_main&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;</span><br><span class="line">    android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;</span><br><span class="line">    android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;</span><br><span class="line">    android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line"></span><br><span class="line">  &lt;CheckBox</span><br><span class="line">      android:id=&quot;@+id/normal_sample_checkbox&quot;</span><br><span class="line">      android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:text=&quot;复选框开关&quot;/&gt;</span><br><span class="line"></span><br><span class="line">  &lt;RadioButton</span><br><span class="line">      android:id=&quot;@+id/normal_sample_radiobutton&quot;</span><br><span class="line">      android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_marginTop=&quot;10dp&quot;</span><br><span class="line">      android:text=&quot;单选按钮&quot;/&gt;</span><br><span class="line"></span><br><span class="line">  &lt;ToggleButton</span><br><span class="line">      android:id=&quot;@+id/normal_sample_togglebutton&quot;</span><br><span class="line">      android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_marginTop=&quot;10dp&quot;/&gt;</span><br><span class="line"></span><br><span class="line">  &lt;Button</span><br><span class="line">      android:id=&quot;@+id/normal_sample_back&quot;</span><br><span class="line">      android:layout_marginTop=&quot;20dp&quot;</span><br><span class="line">      android:text=&quot;退出本页面&quot;</span><br><span class="line">      android:layout_width=&quot;match_parent&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot; /&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<p>3.创建一个单例类来控制模拟点击AccessibilityOperator。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 控制无障碍服务</span><br><span class="line"> * Created by mazaiting on 2017/8/18.</span><br><span class="line"> */</span><br><span class="line">public class AccessibilityOperator &#123;</span><br><span class="line">  private static final String TAG = &quot;AccessibilityOperator&quot;;</span><br><span class="line">  private static AccessibilityOperator mInstance;</span><br><span class="line">  private AccessibilityOperator()&#123;&#125;</span><br><span class="line">  public static AccessibilityOperator getInstance() &#123;</span><br><span class="line">    if (mInstance == null)&#123;</span><br><span class="line">      synchronized (AccessibilityOperator.class)&#123;</span><br><span class="line">        if (mInstance == null)&#123;</span><br><span class="line">          mInstance = new AccessibilityOperator();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return mInstance;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<ol start="4">
<li>创建一个用来模拟点击的界面<br>在要点击的Activity中创建一个Handler来执行延时消息，创建AccessibilityOperator对象，在onCreate方法中获取单例对象。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">private Handler mHandler = new Handler(Looper.getMainLooper());</span><br><span class="line">private AccessibilityOperator accessibilityOperator;</span><br><span class="line"></span><br><span class="line">@Override protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">    .....// 省略布局填充</span><br><span class="line">    accessibilityOperator = AccessibilityOperator.getInstance();</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>并在onResume方法中进行模拟点击。此处只是调用AccessibilityOperator中的方法，因此直接贴代码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">@Override protected void onResume() &#123;</span><br><span class="line">    super.onResume();</span><br><span class="line">    // 执行延时任务</span><br><span class="line">    clickText();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 按文本点击</span><br><span class="line">   */</span><br><span class="line">  private void clickText() &#123;</span><br><span class="line">    clickTextItem(&quot;复选框&quot;,1);</span><br><span class="line">    clickTextItem(&quot;单选按钮&quot;,2);</span><br><span class="line">    clickTextItem(&quot;关闭&quot;,3);</span><br><span class="line">    clickTextItem(&quot;退出本页面&quot;,4);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line">   * 文本单个延时点击</span><br><span class="line">   * @param text 文本内容</span><br><span class="line">   * @param num 延时倍数</span><br><span class="line">   */</span><br><span class="line">  private void clickTextItem(final String text,int num) &#123;</span><br><span class="line">    mHandler.postDelayed(new Runnable() &#123;</span><br><span class="line">      @Override public void run() &#123;</span><br><span class="line">        final boolean isSuccess = accessibilityOperator.clickText(text);</span><br><span class="line">        runOnUiThread(new Runnable() &#123;</span><br><span class="line">          @Override public void run() &#123;</span><br><span class="line">            popToast(isSuccess, text);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,2000*num);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 弹出吐司</span><br><span class="line">   * @param isSuccess</span><br><span class="line">   * @param msg</span><br><span class="line">   */</span><br><span class="line">  private void popToast(boolean isSuccess, String msg) &#123;</span><br><span class="line">    if (isSuccess) &#123;</span><br><span class="line">      Toast.makeText(this, msg + &quot;点击成功&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      Toast.makeText(this, msg + &quot;点击失败&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></p>
<ol start="5">
<li><p>在accessibilityOperator.clickText(text)文本时，系统会先调用AccessibilitySampleService中的onAccessibilityEvent方法，因此在AccessibilityOperator创建一个updateEvent方法，来为AccessibilityService服务与AccessibilityEvent事件赋值。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">private AccessibilityService mAccessibilityService;</span><br><span class="line">private AccessibilityEvent mAccessibilityEvent;</span><br><span class="line">/**</span><br><span class="line"> * 更新事件</span><br><span class="line"> * @param service</span><br><span class="line"> * @param event</span><br><span class="line"> */</span><br><span class="line">public void updateEvent(AccessibilityService service, AccessibilityEvent event) &#123;</span><br><span class="line">  if (mAccessibilityService == null &amp;&amp; service != null)&#123;</span><br><span class="line">    mAccessibilityService = service;</span><br><span class="line">  &#125;</span><br><span class="line">  if (event != null)&#123;</span><br><span class="line">    mAccessibilityEvent = event;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>对AccessibilityService与AccessibilityEvent赋值之后就可以正常使用了。clickText方法的完整内容代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">  /**</span><br><span class="line">   * 根据Text搜索所有符合条件的节点，模糊搜索方式</span><br><span class="line">   * @param text</span><br><span class="line">   * @return</span><br><span class="line">   */</span><br><span class="line">  public boolean clickText(String text) &#123;</span><br><span class="line">    AccessibilityNodeInfo nodeInfo = getRootNodeInfo();</span><br><span class="line">    if (nodeInfo!=null)&#123;</span><br><span class="line">      List&lt;AccessibilityNodeInfo&gt; nodeInfos =</span><br><span class="line">          nodeInfo.findAccessibilityNodeInfosByText(text);</span><br><span class="line">      return performClick(nodeInfos);</span><br><span class="line">    &#125;</span><br><span class="line">    return false;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line">   * 获取根节点</span><br><span class="line">   * @return</span><br><span class="line">   */</span><br><span class="line">  private AccessibilityNodeInfo getRootNodeInfo() &#123;</span><br><span class="line">    Log.e(TAG, &quot;getRootNodeInfo: &quot;);</span><br><span class="line">    AccessibilityEvent curEvent = mAccessibilityEvent;</span><br><span class="line">    AccessibilityNodeInfo nodeInfo = null;</span><br><span class="line">    if (Build.VERSION.SDK_INT &gt;= 16)&#123;</span><br><span class="line">      if (mAccessibilityService!=null)&#123;</span><br><span class="line">        // 获得窗体根节点</span><br><span class="line">        nodeInfo = mAccessibilityService.getRootInActiveWindow();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;else &#123;</span><br><span class="line">      nodeInfo = curEvent.getSource();</span><br><span class="line">    &#125;</span><br><span class="line">    return nodeInfo;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 模拟点击</span><br><span class="line">   * @param nodeInfos</span><br><span class="line">   * @return true 成功； false 失败。</span><br><span class="line">   */</span><br><span class="line">  private boolean performClick(List&lt;AccessibilityNodeInfo&gt; nodeInfos) &#123;</span><br><span class="line">    if (nodeInfos!=null &amp;&amp; !nodeInfos.isEmpty())&#123;// 判断是否非空</span><br><span class="line">      AccessibilityNodeInfo nodeInfo;</span><br><span class="line">      for (int i=0;i&lt;nodeInfos.size();i++)&#123;</span><br><span class="line">        nodeInfo = nodeInfos.get(i);// 获得要点击的View</span><br><span class="line">        // 进行模拟点击</span><br><span class="line">        if (nodeInfo.isEnabled())&#123;// 如果可以点击</span><br><span class="line">          return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return false;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>getRootNodeInfo()返回的AccessibilityNodeInfo可以对它进行遍历，查询它的子节点<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">AccessibilityNodeInfo nodeInfo = getRootNodeInfo();</span><br><span class="line">if (nodeInfo!=null)&#123;</span><br><span class="line">  for (int i=0;i&lt;nodeInfo.getChildCount();i++)&#123;</span><br><span class="line">    AccessibilityNodeInfo child = nodeInfo.getChild(i);</span><br><span class="line">    Log.e(TAG, &quot;clickText: &quot;+child.toString());</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><a href="http://git.oschina.net/zaitingma/codes/6et7fqz34i12wmrxhjvkp65" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-AccessibilityService无障碍服务（一）/" rel="next" title="Android-AccessibilityService无障碍服务（一）">
                <i class="fa fa-chevron-left"></i> Android-AccessibilityService无障碍服务（一）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-AIDL学习（一）/" rel="prev" title="Android-AIDL学习（一）">
                Android-AIDL学习（一） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
